import { pushSalorder, queryViewItem } from '@/api/marketManage/marketOrder'
import { queryPushDelSal } from '@/api/purchaseManagement/purchaseOrder'
import { queryLcCurrency } from '@/api/login'
// 销售订单、预测单、申请单公共方法
export default {
  data() {
    return {
      editPriceRate: [],
      standard: [], // 品质标准
      billtypes: [] // 单据类型
    }
  },
  created() {
    this.currencyDetail()
  },
  methods: {
    // 查询公司默认本位币
    async currencyDetail() {
      await queryLcCurrency().then(res => {
        this.$store.dispatch('tagsView/setData', res.data)
      })
    },
    // 查询单据类型
    async queryTBasBilltype(ev) {
      if (this.billtypes.length > 0) return
      const { data: RES } = await queryViewItem({ viewName: ev || 'SX_V_SAL_BILLTYPE' })
      this.billtypes = RES.list
      RES.list.map(res => {
        if (res.fname === '材料销售订单' || res.fname === '售后销售订单' || res.fname === '样品销售订单') {
          this.editPriceRate.push(res.fid)
        }
      })
    },
    // 品质标准
    async queryFpaezCombo() {
      if (this.standard.length > 0) return
      const { data: RES } = await queryViewItem({ viewName: 'SX_V_SAL_QUALITY' })
      this.standard = RES.list
    },
    // 公共函数
    toExamineFun(api, ev) {
      const DATA = {
        Numbers: [this.prodValue.fbillNo],
        EntryIds: ev === 2 ? this.fentryData : [],
        fIds: ev === 2 ? [this.prodValue.fid] : []
      }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.queryTSalOrderNtry()
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 选择价目表
    stickerHandel(item) {
      if (item.fname) {
        this.prodValue.fpriceListId = item.fid
        this.prodValue.fpriceListName = item.fname
        this.standardPrice.fpriceListId = item.fid
      }
      this.priceVisiblit = false
    },
    // 获取交货方式数据(子传父)
    deliveData(item) {
      if (item.fvalue) {
        this.prodValue.fheadDeliveryWayName = item.fname
        this.prodValue.fheadDeliveryWay = item.fid
      }
      this.deliveVisiblit = false
    },
    // 获取销售员数据(子传父)
    marketData(item) {
      if (item.fuserId) {
        this.prodValue.fsalerName = item.fuserName
        this.prodValue.fsalerId = item.fuserId
      }
      this.marketVisiblit = false
    },
    // 收款条件列表
    gatheringData(item) {
      if (item.fid) {
        this.prodValue.freccondiTionName = item.fname
        this.prodValue.frecConditionId = item.fid
      }
      this.gatheringVisiblit = false
    },
    // 下推采购订单
    pushDown() {
      this.isdanjuDialog = true
      this.purpoorder('SX_PUR_POORDER')
    },
    orderWat(ev) {
      if (ev === this.$t('menu.deliveryRelease')) {
        this.purpoorder('SX_DELIVERYNOTICE')
        this.convert = this.$t('market.xacai')
        this.fbillTypeId = 198
      } else if (ev === this.$t('menu.purOrderAsi')) {
        this.purpoorder('SX_PUR_POORDER')
        this.convert = this.$t('market.xafa')
        this.fbillTypeId = 159
      }
    },
    // 采购订单单据
    async purpoorder(ev) {
      const { data: RES } = await queryViewItem({ viewName: ev })
      this.billTypeData = RES.list
    },
    // 下推采购订单
    pushDownOrder() {
      if (this.fentryData.length === 0) {
        this.saleDetails.forEach(res => {
          this.fentryData.push(res.fentryId)
        })
      }
      if (this.orderLX === this.$t('menu.purOrderAsi')) {
        const DATA = {
          SoucrceIds: [this.prodValue.fid],
          EntryIds: this.fentryData,
          fbillTypeId: this.fbillTypeId
        }
        pushSalorder(DATA).then(res => {
          if (res.code === 0) {
            this.$router.push({
              path: `/editPurchase/${res.data}`
            })
            this.isdanjuDialog = false
          } else {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
          }
        })
      } else if (this.orderLX === this.$t('menu.deliveryRelease')) {
        const DATA = {
          SoucrceIds: [this.prodValue.fid],
          EntryIds: this.fentryData,
          fbillTypeId: this.fbillTypeId
        }
        queryPushDelSal(DATA).then(RES => {
          if (RES.code === 0) {
            this.$router.push({
              path: `/editDelivery/${RES.data}`
            })
          } else {
            this.$confirm(RES.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
          }
        })
        this.isdanjuDialog = false
      }
    }
  }
}
